﻿using ASmile;
using ASmile.Extend;
using System;
using System.Net;
using System.Text;

namespace ASmile.Extern.Lib.Rsp
{
    public abstract class HttpResponseBase : IHttpResponse
    {
        public abstract string Type { get; }
        /// <summary>
        /// 返回当前http上下文上下文
        /// </summary>
        public HttpListenerContext CurrentContext { get; private set; }
        /// <summary>
        /// 获取远程客户端的IP
        /// </summary>
        /// <returns></returns>
        public string GetRemoteIP()
        {
            var ip = CurrentContext.Request.RemoteEndPoint.Address.ToString();
            return ip;
        }
        /// <summary>
        /// 获取 配置文件中自定义配置
        /// </summary>
        public DictExtend GetCustomConfig()
        {
            return Config.Settings.Custom;
        }

        public string GetCustomConfigVal(string key)
        {
            return GetCustomConfig().GetValue(key);
        }

        public T GetCustomConfigVal<T>(string key)
        {
            return GetCustomConfig().GetValue<T>(key);
        }


        public virtual void ReceiveData(HttpListenerContext context, byte[] data, int offset, int count)
        {
            CurrentContext = context;
            string text = null;
            if (data != null && count - offset > 0)
            {
                text = Encoding.UTF8.GetString(data, offset, count);
            }
            ReceiveTextData(context, text);
            LogHelper.LogObj.Info($"响应[{Type}]完成");
        }
        /// <summary>
        /// 结果文本内容的处理方法
        /// </summary>
        protected abstract void ReceiveTextData(HttpListenerContext context, string textData);
        /// <summary>
        /// 返回字节流
        /// </summary>
        protected void ResponseBytes(HttpListenerContext context, byte[] data, int offse, int count)
        {
            try
            {
                using (context.Response.OutputStream)
                {
                    if (data != null && data.Length > 0)
                    {
                        context.Response.OutputStream.Write(data, offse, count);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogObj.Error("响应数据流出错", ex);
            }
        }
        /// <summary>
        /// 响应字节流
        /// </summary>
        protected void ResponseBytes(HttpListenerContext context, byte[] data)
        {
            ResponseBytes(context, data, 0, data.Length);
        }
        /// <summary>
        /// 返回文本内容
        /// </summary>
        protected void ResponseText(HttpListenerContext context, string data)
        {
            byte[] d;
            if (data.IsNotEmpty())
            {
                d = Encoding.UTF8.GetBytes(data);
            }
            else
            {
                d = null;
            }
            ResponseBytes(context, d);
        }
        /// <summary>
        /// 返回一个对象，转换为json格式
        /// </summary>
        protected void ResponseObject(HttpListenerContext context, object data)
        {
            var json = JsonHelper.ToJson(data);
            ResponseText(context, json);
        }
        /// <summary>
        /// 仅发送状态码
        /// </summary>
        protected void ResponseStatus(HttpListenerContext context, HttpStatusCode status, string statusText = null)
        {
            context.Response.StatusCode = ((int)status);
            if (statusText.IsEmpty())
            {
                statusText = status.ToString();
            }
            context.Response.StatusDescription = statusText;
            context.Response.OutputStream.Dispose();
        }
    }

    public abstract class HttpResponseBase<TBody> : HttpResponseBase
        where TBody : class, new()
    {
        protected override void ReceiveTextData(HttpListenerContext context, string textData)
        {
            TBody jsonObj = null;
            if (textData.IsNotEmpty())
            {
                jsonObj = JsonHelper.JsonToObject<TBody>(textData);
            }
            ReceiveJsonData(context, jsonObj);
        }

        protected abstract void ReceiveJsonData(HttpListenerContext context, TBody jsonObj);
    }
}